package com.iqiyi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iqiyi.dto.ActionResult;
import com.iqiyi.po.Episode;
import com.iqiyi.po.Video;
import com.iqiyi.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频的控制层
 *
 * @author YoungKai
 * @date 2021/2/24 11:26
 */
@RequestMapping("/video")
@RestController
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;


    @RequestMapping(value = "/findAllVideo", method = RequestMethod.GET)
    public ActionResult findAllVideo() {
        PageHelper.startPage(0, 5);
        List<Video> videos = videoService.selectAllVideo();
        PageInfo<Video> videoPageInfo = new PageInfo<>(videos);
        return new ActionResult(200, "select successful", videos);
    }

    @RequestMapping(value = "/findAllVideo2", method = RequestMethod.GET)
    public ActionResult findAllVideo2() {
        PageHelper.startPage(1, 4);
        List<Video> videos = videoService.selectAllVideo();
        PageInfo<Video> videoPageInfo = new PageInfo<>(videos);
        return new ActionResult(200, "select successful", videos);
    }


    @RequestMapping(value = "/byVid",method =RequestMethod.GET)
    public ActionResult findVideo(Integer vid){
        Video video = videoService.selectByVid(vid);
        return new ActionResult(200,"successful",video);
    }

    @RequestMapping(value = "/insertVideo",method = RequestMethod.PUT)
    public ActionResult InsertVideo(@RequestBody Video video){
        boolean b = videoService.insertVideo(video);
        if (b==true){
            return new ActionResult(200,"insert successful",null);
        }else {
            return new ActionResult(400,"insert fail",null);
        }
    }

    @RequestMapping(value = "/typeVideos",method = RequestMethod.GET)
    public ActionResult typeVideosAll(){
        List<Video> videos = videoService.selectVideos();
        if(videos!=null) {
            return new ActionResult(200, "successfully", videos);
        }else {
            return new ActionResult(400,"fail",null);
        }
    }

    @RequestMapping(value = "/typeByVid",method = RequestMethod.GET)
    public ActionResult typeByVid(Integer vid){
        Video video = videoService.selectVideoByVid(vid);
        if (video!=null){
            return new ActionResult(200," successful",video);
        }else {
            return new ActionResult(400," fail",null);
        }
    }

    @RequestMapping(value = "/typeLike",method = RequestMethod.GET)
    public ActionResult typeLikeSname(String sname){
        List<Video> videos = videoService.selectSnameByLike(sname);
        if (videos!=null){
            return new ActionResult(200," successful",videos);
        }else {
            return new ActionResult(400," fail",null);
        }
    }


    /**
     * 主页根据影视分类查询符合条件的影视，只显示前5条数据
     */
    @RequestMapping(value = "/findVideoBySort/{sid}",method = RequestMethod.GET)
    public ActionResult findVideoBySort(@PathVariable("sid") Integer sid){
        PageHelper.startPage(0,5);
        List<Video> videoSorts = videoService.selectVideoBySort(sid);
        if(videoSorts != null){
            PageInfo<Video> videoPageInfo = new PageInfo<>(videoSorts);
            return new ActionResult(200,"find success",videoPageInfo);
        }
        return new ActionResult(404,"find null",null);
    }


    /**
     *  主页根据影视分类查询符合条件的影视，只显示前5条数据
     */
    @RequestMapping(value = "/findAllVideoBySort/{sid}",method = RequestMethod.GET)
    public ActionResult findAllVideoBySort(@PathVariable("sid") Integer sid){
        List<Video> videoSorts = videoService.selectVideoBySort(sid);
        if(videoSorts != null){

            return new ActionResult(200,"find success",videoSorts);
        }
        return new ActionResult(404,"find null",null);
    }


    @RequestMapping(value = "/findVideoUrlByVidAndEid", method = RequestMethod.GET)
    public ActionResult findVideoByVidAndEid(int vid, int enumber) {
        log.info("-------vid:" + vid + "-----enumber:" + enumber);
        String videoUrl = videoService.selectVideoByVidAndEid(vid, enumber);
        System.out.println(videoUrl);
        return new ActionResult(200, "find success!", videoUrl);
    }
    /**
     * 通过vid查询视频地址
     *
     * @param vid
     * @return
     */
    @RequestMapping(value = "/findVideoUrlByVid", method = RequestMethod.GET)
    public ActionResult findVideoUrlByVid(int vid) {
        log.info("-------vid:" + vid);
        List<String> videoUrls = videoService.findVideoUrlByVid(vid);
        String videoUrl = videoUrls.get(0);
        System.out.println(videoUrl);
        return new ActionResult(200, "find success!", videoUrl);
    }

    @RequestMapping(value = "/findVideoByVid", method = RequestMethod.GET)
    public ActionResult findVideoByVid(int vid) {
        log.info("-----" + vid);
        Video videoByVid = videoService.findVideoByVid(vid);
        log.info("----" + videoByVid);
        return new ActionResult(200, "find success!", videoByVid);
    }
}
